package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.FaqCategories;

public class GenericFaqCategoriesDAO extends AutoKeys implements
		org.jetnuke.dao.FaqCategoriesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericFaqCategoriesDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.FaqCategoriesModel#retrieveByPK(int)
	 */
	public FaqCategories retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("FaqCategoriesModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		FaqCategories s = new FaqCategories();

		if (rs.next()) {
			fillFaqCategoriesFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillFaqCategoriesFromResultSet(FaqCategories s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillFaqCategoriesFromResultSet(FaqCategories, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCATEGORIES(rs.getString("CATEGORIES"));
		s.setFLANGUAGE(rs.getString("FLANGUAGE"));
		s.setID_CAT(rs.getInt("ID_CAT"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillFaqCategoriesFromResultSet(FaqCategories, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.FaqCategoriesModel#update(org.jetnuke.entities.FaqCategories)
	 */
	public void update(FaqCategories cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(FaqCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("FaqCategoriesModel.update"));

		p.setString(1, cat.getCATEGORIES());
		p.setString(2, cat.getFLANGUAGE());
		p.setInt(3, cat.getID_CAT());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(FaqCategories) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.FaqCategoriesModel#addNew(org.jetnuke.entities.FaqCategories)
	 */
	public int addNew(FaqCategories cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(FaqCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("FaqCategoriesModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("FaqCatModel.lastGeneratedFaqCatID"));
		initFaqCat(cat, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(FaqCategories) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param cat
	 * @param p
	 */
	private void initFaqCat(FaqCategories cat, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initFaqCat(FaqCategories, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, cat.getCATEGORIES());
		p.setString(2, cat.getFLANGUAGE());

		if (logger.isDebugEnabled()) {
			logger.debug("initFaqCat(FaqCategories, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.FaqCategoriesModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("FaqCategoriesModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			FaqCategories u = new FaqCategories();
			fillFaqCategoriesFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.FaqCategoriesModel#deleteFaqCatByID(int)
	 */
	public void deleteFaqCatByID(int catid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteFaqCatByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("FaqCategoriesModel.deleteFaqCatByID"));
		p.setInt(1, catid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteFaqCatByID(int) - end"); //$NON-NLS-1$
		}
	}

}
